package com.phelon.epdm;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.BooleanUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import com.phelon.systemLink.SessionInfo;
import com.phelon.systemLink.SessionManager;

public class RoutingXMLBuilder extends XMLBuilder {

	private Routing 	theRouting;
	
	public static void main(String[] args) {
		Routing testBom = new Routing("A  ", "ASP9802536  ", "      ");
		RoutingXMLBuilder builder = new RoutingXMLBuilder(testBom, "7M");
		builder.doCreate();
		System.exit(0);
	}
	
	public RoutingXMLBuilder(Routing theRouting, String toEnv) {
		this.theRouting = theRouting;
		setEnvironmentID(toEnv);
	}
	
	public void buildCreateXML(SessionInfo theSession) {
		setDocument(DocumentHelper.createDocument());
		
		getDocument().addDocType("System-Link", "SYSTEM", "SystemLinkRequest.dtd");
		
		Element root = getDocument().addElement("System-Link").addAttribute("formatForDisplay", "true");
		
		Element request = root.addElement("Request").addAttribute("sessionHandle"
				,theSession == null ? "*current" : theSession.getSessionHandle()).addAttribute(
				"workHandle", "*new").addAttribute("broker","EJB").addAttribute("maxIdle", "1000");
		Element domainEntity = request.addElement("Create").addAttribute("name",
				"queryObject_Routing").addAttribute("domainClass","com.mapics.epdm.Routing")
				.addAttribute("retainResult", "true").addElement("DomainEntity");
		Element key = domainEntity.addElement("Key");
		key.addElement("Property").addAttribute("path", "site").addElement("Value")
				.addCDATA(theRouting.getSite());
        key.addElement("Property").addAttribute("path", "routingIdentifier").addElement("Value")
        		.addCDATA(theRouting.getRoutingIdentifier());
        key.addElement("Property").addAttribute("path", "routingVersion").addElement("Value")
        		.addCDATA(theRouting.getRoutingRevision());
        domainEntity.addElement("Property").addAttribute("path", "description").addElement("Value").addCDATA(
                theRouting.getDescription());
        domainEntity.addElement("Property").addAttribute("path", "userFieldDate1").addElement("Value").addCDATA(
                theRouting.getUserFieldDate1());
/*        domainEntity.addElement("Property").addAttribute("path", "userFieldSwitchA").addElement("Value").addCDATA(
                theRouting.getUserFieldSwitchA()); */	
        domainEntity.addElement("Property").addAttribute("path", "userFieldText40").addElement("Value").addCDATA(
                theRouting.getUserFieldText40());
        for (RoutingOperation oper : theRouting.getOperations()){
    		domainEntity = request.addElement("Create").addAttribute("name","line" + 
    				oper.getOperationSequence()).addAttribute("domainClass","com.mapics.epdm.RoutingOperation")
    				.addAttribute("retainResult", "true").addElement("DomainEntity");
			key = domainEntity.addElement("Key");
			key.addElement("Property").addAttribute("path", "site").addElement("Value")
					.addCDATA(oper.getSite());
		    key.addElement("Property").addAttribute("path", "routingIdentifier").addElement("Value")
		    		.addCDATA(oper.getRoutingIdentifier());
		    key.addElement("Property").addAttribute("path", "routingVersion").addElement("Value")
		    		.addCDATA(oper.getRoutingVersion()); 
			key.addElement("Property").addAttribute("path", "operationSequence").addElement("Value")
					.addCDATA(oper.getOperationSequence());
	        domainEntity.addElement("Property").addAttribute("path", "averageOutsideOperationCost").addElement("Value").addCDATA(
	                oper.getAverageOutsideOperationCost());        
	        domainEntity.addElement("Property").addAttribute("path", "currentOperationYield").addElement("Value").addCDATA(
	                oper.getCurrentOperationYield());
	        domainEntity.addElement("Property").addAttribute("path", "description").addElement("Value").addCDATA(
	                oper.getDescription());
	        domainEntity.addElement("Property").addAttribute("path", "facility").addElement("Value").addCDATA(
	                oper.getFacility());
	        domainEntity.addElement("Property").addAttribute("path", "inputBufferHours").addElement("Value").addCDATA(
	                oper.getInputBufferHours());
	        domainEntity.addElement("Property").addAttribute("path", "moveDays").addElement("Value").addCDATA(
	                oper.getMoveDays());
	        domainEntity.addElement("Property").addAttribute("path", "numberOfUnitsAtThisOperation").addElement("Value").addCDATA(
	                oper.getNumberOfUnitsAtThisOperation());
	        domainEntity.addElement("Property").addAttribute("path", "operationStatus").addElement("Value").addCDATA(
	                oper.getOperationStatus());
	        domainEntity.addElement("Property").addAttribute("path", "outputBufferHours").addElement("Value").addCDATA(
	                oper.getOutputBufferHours());
	        domainEntity.addElement("Property").addAttribute("path", "outsideOperationCost").addElement("Value").addCDATA(
	                oper.getOutsideOperationCost());
	        domainEntity.addElement("Property").addAttribute("path", "processSheet").addElement("Value").addCDATA(
	                oper.getProcessSheet());
	        domainEntity.addElement("Property").addAttribute("path", "reportingPoint").addElement("Value").addCDATA(
	                oper.getReportingPoint());
	        domainEntity.addElement("Property").addAttribute("path", "resourceConstraintOne").addElement("Value").addCDATA(
	                oper.getResourceConstraintOne());
	        domainEntity.addElement("Property").addAttribute("path", "resourceConstraintTwo").addElement("Value").addCDATA(
	                oper.getResourceContraintTwo());
	        domainEntity.addElement("Property").addAttribute("path", "routingOperationBitmapOrIcon").addElement("Value").addCDATA(
	                oper.getRoutingOperationBitmapOrIcon());
	        domainEntity.addElement("Property").addAttribute("path", "routingPrintFlag").addElement("Value").addCDATA(
	                oper.getRoutingPrintFlag());
	        domainEntity.addElement("Property").addAttribute("path", "runLaborTimeUseTbc").addElement("Value").addCDATA(
	                oper.getRunLaborTimeUseTbc());
	        domainEntity.addElement("Property").addAttribute("path", "runMachineTimeUseTbc").addElement("Value").addCDATA(
	                oper.getRunMachineTimeUseTbc());
	        domainEntity.addElement("Property").addAttribute("path", "selectNumber").addElement("Value").addCDATA(
	                oper.getSelectNumber());
	        domainEntity.addElement("Property").addAttribute("path", "setupCrewSize").addElement("Value").addCDATA(
	                oper.getSetupCrewSize());
	        domainEntity.addElement("Property").addAttribute("path", "setupLaborHours").addElement("Value").addCDATA(
	                oper.getSetupLaborHours());
	        domainEntity.addElement("Property").addAttribute("path", "standardOperationYield").addElement("Value").addCDATA(
	                oper.getStandardOperationYield());
	        domainEntity.addElement("Property").addAttribute("path", "timeBasisCode").addElement("Value").addCDATA(
	                oper.getTimeBasisCode());
	        domainEntity.addElement("Property").addAttribute("path", "toolNumber").addElement("Value").addCDATA(
	                oper.getToolNumber());
	        domainEntity.addElement("Property").addAttribute("path", "transactionCode").addElement("Value").addCDATA(
	                oper.getTransactionCode());
	        domainEntity.addElement("Property").addAttribute("path", "userFieldCodeA").addElement("Value").addCDATA(
	                oper.getUserFieldCodeA());
	        domainEntity.addElement("Property").addAttribute("path", "userFieldCurrencyA").addElement("Value").addCDATA(
	                oper.getUserFieldCurrencyA());
	        domainEntity.addElement("Property").addAttribute("path", "userFieldQuantity1").addElement("Value").addCDATA(
	                oper.getUserFieldQuantity1()); 
	        domainEntity.addElement("Property").addAttribute("path", "userFieldDate1").addElement("Value").addCDATA(
	                oper.getUserFieldDate1());
/*	        domainEntity.addElement("Property").addAttribute("path", "userFieldSwitchA").addElement("Value").addCDATA(
	                oper.getUserFieldSwitchA()); */
	        domainEntity.addElement("Property").addAttribute("path", "userFieldText40").addElement("Value").addCDATA(
	                oper.getUserFieldText40());
        }       
 	}  
	

	public void readSuccessfulResponse(Document responseDocument) {
		// TODO Auto-generated method stub
		
	}
}